import threading


class ZeroEvenOdd:
    def __init__(self, n):
        self.n = n
        self.s1 = threading.Semaphore(1)
        self.s2 = threading.Semaphore(0)
        self.s3 = threading.Semaphore(0)

    def zero(self):
        for i in range(0, self.n):
            self.s1.acquire()
            print('zero:', 0)
            if i % 2 == 0:
                self.s3.release()
            else:
                self.s2.release()

    def even(self):
        for i in range(2, self.n + 1, 2):
            self.s2.acquire()
            print('even:', i)
            self.s1.release()

    def odd(self):
        for i in range(1, self.n + 1, 2):
            self.s3.acquire()
            print('odd:', i)
            self.s1.release()


if __name__ == '__main__':
    z = ZeroEvenOdd(5)
    t1 = threading.Thread(target=z.zero)
    t2 = threading.Thread(target=z.even)
    t3 = threading.Thread(target=z.odd)
    t1.start()
    t2.start()
    t3.start()
